let app = getApp();
var sourceType = [
  ['camera'],
  ['album'],
  ['camera', 'album']
]
var sizeType = [
  ['compressed'],
  ['original'],
  ['compressed', 'original']
]

Page({
  data: {
    openid: '',
    imageList: [],
    imageFileArror: [],
    sourceTypeIndex: 2,
    sourceType: ['拍照', '相册', '拍照或相册'],
    sizeTypeIndex: 0,
    sizeType: ['压缩', '原图', '压缩或原图'],
    countIndex: 3,
    count: [1, 2, 3, 4],
    customerid: 0,
    customerState: ['A', 'B', 'C', 'D'],
    Rankid: 0,
    customerRank: ['A', 'B', 'C', 'D'],
    reportTitle: '',
    reportComment: ''
  },
  onLoad() {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openid: res.data
        })
      },
    })
  },
  StateChange(e) {
    this.setData({
      customerid: e.detail.value
    })
  },
  RankChange(e) {
    this.setData({
      Rankid: e.detail.value
    })
  },
  chooseImage() {
    var that = this;
    wx.chooseImage({
      sourceType: sourceType[this.data.sourceTypeIndex],
      sizeType: sizeType[this.data.sizeTypeIndex],
      count: this.data.count[this.data.countIndex],
      success(res) {
        console.log(res)
        let tempArr = res.tempFiles,
          tempNewArr = [],
          imgFileArr = [];
        for (let i in tempArr) {
          if (tempArr[i].size <= 2000000) {
            tempNewArr.push(tempArr[i].path);
          } else {
            let numbers = Number(i) + 1;
            app.toast('上传的第' + numbers + '张图片过大,请重新选择');
            return false;
          }
        }
        that.setData({
          imageList: tempNewArr,
          //imageFileArror: imgFileArr
        });
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  previewImage(e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  //标题
  reportTitle(e) {
    this.setData({
      reportTitle: e.detail.value
    });
  },
  //内容
  reportComment(e) {
    this.setData({
      reportComment: e.detail.value
    });
  },
  //提交
  submitReport() {
    let that = this,
      title = that.data.reportTitle,
      comment = that.data.reportComment,
      imgUrl = that.data.imageList,
      imgFileList = [];
    for (let i in imgUrl) {
      let tempBase64 = wx.getFileSystemManager().readFileSync(imgUrl[i], "base64");
      imgFileList.push(tempBase64);
    }
    if (!title) {
      app.toast('汇报标题不可为空');
    } else if (!comment) {
      app.toast('汇报内容不可为空');
    } else if (imgFileList.length === 0) {
      app.toast('请上传图片');
    } else {

      app.post('GenZong', {
        openid: that.data.openid,
        biaoti: that.data.reportTitle, //标题
        neirong: that.data.reportComment, //内容
        ZhaoPianbase641: imgFileList[0] ? imgFileList[0] : '', // 默认4个照片
        ZhaoPianbase642: imgFileList[1] ? imgFileList[1] : '', // 默认4个照片
        ZhaoPianbase643: imgFileList[2] ? imgFileList[2] : '', // 默认4个照片
        ZhaoPianbase644: imgFileList[3] ? imgFileList[3] : '' // 默认4个照片
      }).then((res) => {
        if (res.msg === '跟踪提交成功！') {
          wx.reLaunch({
            url: '/pages/report/report',
          });
          app.toast('添加汇报成功');
        } else {
          app.toast('添加汇报失败');
        }

      });
    }
  }
})